﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Entity;

namespace Runda.CMS.Portal.ContractFollowUp
{
    public partial class InstrumentConservation_config_edit : BasePage
    {
        private SysDictionaryBll sysBll = new SysDictionaryBll();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                initForm();
            }
        }

        private void initForm()
        {
            if (Request.QueryString["Param"] != null && Request.QueryString["Param"] != "")
            {
                WsmConservationEntity sysDicEntity = sysBll.Get<WsmConservationEntity>(new Guid(Request.QueryString["Param"].ToString()));
                txtCode.Text = sysDicEntity.ConservationName;             
                txtparmGUID.Value = Request.QueryString["Param"].ToString();
                if (sysDicEntity.OrderNum != null)
                    orderNum.Value = System.Convert.ToDouble(sysDicEntity.OrderNum);               
            }           
        }

        private bool save(ref string message)
        {
            WsmConservationEntity sysDicEntity;
            if (Request.QueryString["Param"] != null && Request.QueryString["Param"] != "")
            {
                sysDicEntity = sysBll.Get<WsmConservationEntity>(new Guid(txtparmGUID.Value));
            }
            else
            {
                sysDicEntity = new WsmConservationEntity();
                sysDicEntity.CreatedBy = CurrentUserID;
                sysDicEntity.CreatedOn = System.DateTime.Now;
                sysDicEntity.Deleted = false;
            }
            sysDicEntity.ConservationName = txtCode.Text;
            if (orderNum.Value != null)
            {
                sysDicEntity.OrderNum = System.Convert.ToInt32(orderNum.Value);
            }
            else {
                List<WsmConservationEntity> allList = sysBll.GetAll<WsmConservationEntity>(" and 1=1");
                if (allList != null && allList.Count > 0)
                {
                    sysDicEntity.OrderNum = 10 * allList.Count;
                }
                else {
                    sysDicEntity.OrderNum = 1;
                }
            }
            sysBll.InsertOrUpdate(sysDicEntity);
       
            return true;
        }

        protected void lnkbtnSaveAndNew_Click(object sender, EventArgs e)
        {
            string Message = "";
            if (!save(ref Message))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "saveansrror", "alert('" + Message + "');", true);
            }
            else
            {
                txtCode.Text = "";
                txtparmGUID.Value = "";
                ClientScript.RegisterStartupScript(this.GetType(), "saveandnew", "alert('保存成功');", true);
            }
        }

        protected void lnkbtnSaveAndClose_Click(object sender, EventArgs e)
        {
            string Message = "";
            if (!save(ref Message))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "saveansrror", "alert('" + Message + "');", true);
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('保存成功');window.close();", true);
            }
        }
    }
}
